﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Actions = AEM.Data.BLL.Actions;

namespace AEM.Basic
{
    /// <summary>
    /// Interaction logic for 
    /// </summary>
    public partial class FaultDefinition : Window
    {
        public FaultDefinition()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(FaultDefinition_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(FaultDefinition_Loaded);
            this.faultList.SelectionChanged += new SelectionChangedEventHandler(faultList_SelectionChanged);
            this.faultAdd.Click += new RoutedEventHandler(faultAdd_Click);
            this.faultEdit.Click += new RoutedEventHandler(faultEdit_Click);
            this.faultDelete.Click += new RoutedEventHandler(faultDelete_Click);
        }

        void FaultDefinition_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void FaultDefinition_Loaded(object sender, RoutedEventArgs e)
        {
            this.faultList.ItemsSource = Data.DAL.FaultDefinition.List("");
        }

        void faultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.Model.FaultDefinition errrow = (sender as ListView).SelectedItem as Data.Model.FaultDefinition;
            if (errrow == null) { return; }
            faultCode.Text = errrow.FaultCode;
            faultName.Text = errrow.FaultName;
            faultReason.Text = errrow.FaultReason;
        }

        void faultAdd_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (string.IsNullOrEmpty(faultCode.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("FaultCode") as string); return; }
            if (string.IsNullOrEmpty(faultName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("FaultName") as string); return; }

            Data.Model.FaultDefinition error = new Data.Model.FaultDefinition();
            error.FaultCode = faultCode.Text;
            error.FaultName = faultName.Text;
            error.FaultReason = faultReason.Text;
            AEM.Data.BLL.Actions.FaultDefinition.AddFaultResult ret = Data.BLL.Actions.FaultDefinition.AddFault(error);
            switch (ret)
            {
                case Actions.FaultDefinition.AddFaultResult.FaultIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("FaultInfoAdd") as string);
                    break;
                case Actions.FaultDefinition.AddFaultResult.FaultNameIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("FaultName") as string);
                    break;
                case Actions.FaultDefinition.AddFaultResult.FaultCodeExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("FaultCode") as string);
                    break;
                case Actions.FaultDefinition.AddFaultResult.FaultIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("FaultInfoAdd") as string);
                    break;
                case Actions.FaultDefinition.AddFaultResult.OK:
                    this.faultList.Items.Refresh();
                    errmsg.Text = this.TryFindResource("successfuladd") as string;
                    faultCode.Text = string.Empty;
                    faultName.Text = string.Empty;
                    faultReason.Text = string.Empty;
                    break;
            }
        }

        void faultEdit_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (string.IsNullOrEmpty(faultCode.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("FaultCode") as string); return; }
            if (string.IsNullOrEmpty(faultName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("FaultName") as string); return; }

            Data.Model.FaultDefinition fault = this.faultList.SelectedItem as Data.Model.FaultDefinition;
            fault.FaultCode = faultCode.Text.Trim();
            fault.FaultName = faultName.Text.Trim();
            fault.FaultReason = faultReason.Text.Trim();
            Actions.FaultDefinition.ModifiyFaultResult ret = Data.BLL.Actions.FaultDefinition.ModifiyFault(fault);
            switch (ret)
            {
                case Actions.FaultDefinition.ModifiyFaultResult.FaultIsNotExist:
                    errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("FaultInfoAdd") as string);
                    break;
                case Actions.FaultDefinition.ModifiyFaultResult.FaultIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("FaultInfoAdd") as string);
                    break;
                case Actions.FaultDefinition.ModifiyFaultResult.FaultNameAlreadyExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("FaultName") as string);
                    break;
                case Actions.FaultDefinition.ModifiyFaultResult.OK:
                    this.faultList.Items.Refresh();
                    errmsg.Text = this.TryFindResource("successfulupdate") as string;
                    faultCode.Text = string.Empty;
                    faultName.Text = string.Empty;
                    faultReason.Text = string.Empty;
                    break;
            }
        }

        void faultDelete_Click(object sender, RoutedEventArgs e)
        {
            Data.Model.FaultDefinition fault = this.faultList.SelectedItem as Data.Model.FaultDefinition;
            if (null == fault) { errmsg.Text = "请选择你要删除的故障信息"; return; }
            if (MessageBoxResult.Yes == MessageBox.Show("确定要删除记录:" + fault.FaultCode + "(" + fault.FaultName + ")" + " 吗？", TryFindResource("MessageBoxTitle")as string, MessageBoxButton.YesNo, MessageBoxImage.Warning))
            {
                Data.BLL.Actions.FaultDefinition.DeleteFaultResult ret = Data.BLL.Actions.FaultDefinition.doDeleteFault(fault.ID);
                switch (ret)
                {
                    case Actions.FaultDefinition.DeleteFaultResult.FaultIsNotExist:
                        errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("FaultInfoAdd") as string);
                        break;
                    case Actions.FaultDefinition.DeleteFaultResult.OK:
                        errmsg.Text = this.TryFindResource("successfuldelete") as string;
                        this.faultList.Items.Refresh();
                        faultCode.Text = string.Empty;
                        faultName.Text = string.Empty;
                        faultReason.Text = string.Empty;
                        break;
                }
            }
        }
    }
}
